<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html; charset=UTF-8" %>
<script>
    jQuery(function ($) {
        $("#editBtn").click(function () {
            let $selected = $(":checkbox[name=id]:checked");
            if ($selected.size() != 1) {
                alert("必须且只能选择一个！");
                return;
            }
            //location.href = '/type/edit.html?code=' + $selected.val();
            loadMain('/type/edit.html?code=' + $selected.val());
        });

        $("#delBtn").click(function () {
            let $selected = $(":checkbox[name=id]:checked");
            let codes = $selected.map(function () {
                return this.value;
            }).get();

            // 将数组中的元素使用”,“连接
            codes = codes.join(",");
            $.ajax({
                url: '/value/getHasRef?codes=' + codes,
                // codes: 1,2,3
                // data: 1,2
                success(data) {
                    // 返回的数据如果不是数组，说明出错了
                    if (data.length == undefined) return;

                    if (data.length) {
                        alert("编码为【" + data + "】的字典类型下存在字典值，请先删除该类型下的字典值！");
                    } else {
                        if (confirm("确定删除吗？")) {
                            //location.href = "/type/delete?codes=" + codes;
                            loadMain("/type/delete?codes=" + codes);
                        }
                    }
                }
            })
        });

        /*$("#selectAll").click(function () {
            $(":checkbox[name=id]").prop("checked", this.checked);
        });

        $(":checkbox[name=id]").click(function () {
            /!*let checked = true;
            $(":checkbox[name=id]").each(function () {
                if (!this.checked) {
                    checked = false;
                }
            })
            $("#selectAll").prop("checked", checked);*!/

            //$("#selectAll").prop("checked", $(":checkbox[name=id]:not(:checked)").size() == 0);
            $("#selectAll").prop("checked", $(":checkbox[name=id]").size() == $(":checkbox[name=id]:checked").size());
        });*/
    });
</script>

<div class="page-header">
    <h3>字典类型列表</h3>
</div>
<div class="btn-toolbar" role="toolbar" style="height:50px;">
    <button class="btn btn-primary" url="/dictionary/type/save.html">
        <span class="glyphicon glyphicon-plus"></span> 创建
    </button>
    <button class="btn btn-default" id="editBtn">
        <span class="glyphicon glyphicon-edit"></span> 编辑
    </button>
    <button class="btn btn-danger" id="delBtn">
        <span class="glyphicon glyphicon-minus"></span> 删除
    </button>
</div>
<div>
    <table class="table table-hover table-striped">
        <thead>
        <tr style="background-color:#d9edf7">
            <th><input id="selectAll" type="checkbox"/></th>
            <th>序号</th>
            <th>编码</th>
            <th>名称</th>
            <th>描述</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${typeList}" var="type" varStatus="sta">
            <tr>
                <td><input name="id" value="${type.code}" type="checkbox"/></td>
                <td>${sta.count}</td>
                <td>${type.code}</td>
                <td>${type.name}</td>
                <td>${type.description}</td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>